package com.java2007.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.pojo.OrderDetail;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.service.PayService;
import com.java2007.hotel.service.impl.DinnerTableServiceImpl;
import com.java2007.hotel.service.impl.OrderServiceImpl;
import com.java2007.hotel.service.impl.PayServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
@WebServlet("/pay")
public class PayController extends Controller {
    private PayService payService = new PayServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    private DinnerTableService dinnerTableService = new DinnerTableServiceImpl();


    /**
     * 订单支付
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {


        HttpSession session = request.getSession();

        Order order = (Order) session.getAttribute("order");
        //获取餐桌id，判断餐桌是否存在
        DinnerTable dinnerTableByTableId = dinnerTableService.findDinnerTableByTableId(order.getOrderTableId());
        if (dinnerTableByTableId == null){
            session.removeAttribute("order");
            session.removeAttribute("totalPrice");
            session.removeAttribute("orderDetailsList");
            session.removeAttribute("tableId");
            return ServletConstant.jump("餐桌不存在请重写下单", "index.jsp");
        }
        if (dinnerTableByTableId.getTableStatus() == 0 ){
            session.removeAttribute("order");
            session.removeAttribute("totalPrice");
            session.removeAttribute("orderDetailsList");
            session.removeAttribute("tableId");
            return ServletConstant.jump("餐桌已被修改，请重新下单", "index.jsp");
        }


        //获取订单id
        Long orderId = order.getOrderId();
        Order orderFind = orderService.findOrder(orderId);
        if (orderFind.getOrderPayStatus() == 1){
            //修改订单状态
            session.removeAttribute("order");
            session.removeAttribute("totalPrice");
            session.removeAttribute("orderDetailsList");
            session.removeAttribute("tableId");

            return ServletConstant.jump("已经支付，不要重复提交", "/index.jsp");
        }

        //获取订单中的总价

        Long orderPrice = order.getOrderPrice();


        //获取用户余额
        User user = (User) session.getAttribute("user");
        Long userMoney = user.getUserMoney();

        if (orderPrice > userMoney || userMoney == null) {
            return ServletConstant.jump("余额不足，请充值", "front/detail/clientOrderList.jsp");
        }
        User userNew = payService.pay(order, user);
        if (userNew == null) {
            return ServletConstant.jump("支付失败", "front/detail/clientOrderList.jsp");
        }




        //修改订单状态
        order.setOrderPayStatus(1);

        orderService.updateOrder(order);
        session.setAttribute("user", userNew);
        session.removeAttribute("order");
        session.removeAttribute("totalPrice");
        session.removeAttribute("orderDetailsList");
        session.removeAttribute("tableId");

        return ServletConstant.jump("支付成功", "front?method=findDinnerTableByStatus");
    }


}